﻿using System;

namespace XUtility
{
    #region Decimal 类型 扩展方法

    public static class DecimalEx
    {
        #region 返回 Double 类型
        /// <summary>
        /// 返回 Double 类型
        /// </summary>
        /// <param name="d">输入值</param>
        /// <returns>Double 类型</returns>
        public static Double ToDouble(this Decimal d)
        {
            return d.ToDouble(0.0);
        }
        /// <summary>
        /// 返回 Double 类型
        /// </summary>
        /// <param name="d">输入值</param>
        /// <param name="v">默认值</param>
        /// <returns>Double 类型</returns>
        public static Double ToDouble(this Decimal d, Double v)
        {
            Double result;
            if(!Double.TryParse(d.ToString(), out result))
            {
                result = v;
            }
            return result;
        }
        /// <summary>
        /// 返回 Double 类型
        /// </summary>
        /// <param name="d">输入值</param>
        /// <param name="v">默认值</param>
        /// <returns>Double 类型</returns>
        public static Double ToDouble(this Decimal? d)
        {
            Double result = 0D;
            if (d.HasValue)
                result = d.Value.ToDouble();
            return result;
        }
        #endregion
    }
    #endregion
}
